define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'email/index' + location.search,
                    add_url: 'email/add',
                    edit_url: 'email/edit',
                    del_url: 'email/del',
                    multi_url: 'email/multi',
                    table: 'email',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'nickname', title: __('User_id')},
                        {field: 'name', title: __('Price_id')},
                        {field: 'custom', title: __('Custom'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'images', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'collect_num', title: __('Collect_num')},
                        {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1')}, formatter: Table.api.formatter.normal},
                        {field: 'send_status', title: '是否已经寄出', searchList: {"0":'未寄出',"1":'已寄出'}, formatter: Table.api.formatter.normal},
                        {field: 'switch', title: __('Switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'receive_time', title: '收件时间',operate:false,sortable: true},
                        {field: 'createtime', title: __('Createtime'),operate:false,sortable: true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        
                        {field: 'id', title: __('发送信件'), table: table, buttons: [
                            {
                                name: 'detail',
                                text: '发送信件',
                                title:__('发送信件'),
                                icon: 'fa fa-send', 
                                classname: 'btn btn-xs btn-success btn-ajax', 
                                url: 'email/sendMail', 
                                success:function(data, ret){
                                    window.location.href = document.location;
                                }, 
                                error:function(){}
                            },
                        ], 
                        operate:false, formatter: Table.api.formatter.buttons},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});